//
//  ColumnViewController.m
//  PlayerFix
//
//  Created by dllo on 15/11/4.
//  Copyright © 2015年 DAT. All rights reserved.
//

#import "ColumnViewController.h"
#import "ColumnTableViewCell.h"
#import "ColumnModel.h"
#import "CollumnListViewController.h"
@interface ColumnViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *myTableView;

@property (nonatomic, strong) NSMutableArray *allArray;

@end

@implementation ColumnViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.allArray = [NSMutableArray array];
    
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 375, 667 - 94) style:UITableViewStylePlain];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:self.myTableView];
    [self addHeaderRefresh];

}

- (void)addHeaderRefresh
{
    self.myTableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [self getAfnModel];
    }];
    
    [self.myTableView.header beginRefreshing];
}

- (void)getAfnModel{
    
    [DATAsyncGetAFNBlock GetAFNURLWithString:@"http://api.gougouvideo.com:8888/topics/index.json?v=1.7.0" andBody:nil andCookie:nil resultBlock:^(id result) {
        
        NSArray *bigArray = result;
        for (NSDictionary *dic in bigArray) {
            ColumnModel *columnModel = [[ColumnModel alloc] init];
            [columnModel setValuesForKeysWithDictionary:dic];
            [self.allArray addObject:columnModel];
        }
        [self.myTableView reloadData];
        [self.myTableView.header endRefreshing];
    } errorBlock:^(NSError *error) {
        [self.myTableView.header endRefreshing];
        NSLog(@"columnError = %@",error);
        
    }];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//    NSLog(@"===%ld",self.allArray.count);
    return self.allArray.count;
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifier = @"columnCell";
    ColumnTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[ColumnTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    cell.columnModel = [_allArray objectAtIndex:indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    ColumnModel *columnModel = [_allArray objectAtIndex:indexPath.row];
    CollumnListViewController *columnListVC = [[CollumnListViewController alloc] init];
    [[SingletonForPush shareSingleton].naVC pushViewController:columnListVC animated:YES];
    columnListVC.url = columnModel.apiurl;
    columnListVC.name = columnModel.title;
    [self.navigationController pushViewController:columnListVC animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
